/*
 Reff Link : http://www.thelearningpoint.net/computer-science/arrays-and-sorting-bubble-sort--with-c-program-source-code
*/

#include <stdio.h>

#if 0
void bubblesort (int *array, int len)
{
	int i, j, t, swapped = 0;
		
	for (i=0; i<len; i++)
	{
		for (j=0; j<len-1; j++)
		{
			if (array[j] > array[j+1])
			{
				t = array[j];
				array[j] = array[j+1];
				array[j+1] = t;
				swapped++;
				printf ("\n Swapps used : %d", swapped);
			}
		}
	}
//	printf ("\n Swapps used : %d", swapped);
}
#endif

#if 1
void bubblesort (int *array,int number_of_elements)
{
        int iter, temp, swapped;
        do
        {
                swapped = 0; /* If no element is swapped array is sorted */
                /* In the following loop compare every pair of adjacent elements and check
                   if they are in correct order */
                for(iter = 1; iter < number_of_elements; iter++)
                {
                        if(array[iter-1] > array[iter])
                        {
                                temp = array[iter-1];
                                array[iter-1] = array[iter];
                                array[iter] = temp;
                                swapped = 1;
				printf ("\n Swapps used : %d", swapped);
                        }
                }
//		printf ("\n Swapps used : %d", swapped);

        }while(swapped);
//	printf ("\n Swapps used : %d", swapped);
}
#endif 

main ()
{
	int i, len, arr[100], arr1[100];	

	printf ("Enter array len :\t");
	scanf ("%d", &len);

	for (i=0; i<len; i++)
	{
		printf ("\nenter %d element :\t", i);
		scanf ("%d", &arr[i]);
	}

	bubblesort (arr, len);
	
	printf ("\n After sorting arr is : \t");
	for (i=0; i<len; i++)
	{
		printf ("%d ", arr[i]);
	}
}
